<?php
namespace Seller\Controller;
use Think\Controller;

class IndexController extends CommonController
{
    public function _initialize()
    {
        $params = array("index" ,"self" ,'changetableval',"welcome","no_auth");
        parent::_initialize($params);
    }

    public function index()
    {
        $this->display();
    }

    public function welcome()
    {
        $shop_id = session("shop_id");
        // 订单参数
        $order_info["order_num"] = M("order")->where(array("shop_id"=>$shop_id))->count();
        $order_info["order_complete"] = M("order")->where("`status` = 3 AND `shop_id` = $shop_id")->count();
        $order_info["order_for_deliver"] = M("order")->where("`is_pay` = 1 AND `is_deliver` = '-1' AND `shop_id` = $shop_id")->count();
        $order_info["order_nopay"] = M("order")->where("`is_pay` = '-1' AND `shop_id` = $shop_id")->count();
        $order_info["order_is_platform"] = M("order")->where(array('is_platform'=>1,'is_deliver'=>-1,'is_pay'=>1,'status'=>'0','shop_id'=>$shop_id))->count();
        $order_info["order_calcel"] = M("order")->where("status = '-1' AND shop_id = $shop_id")->count();
        $order_info["order_for_confirm"] = M("order")->where("`status` = 1 AND shop_id = $shop_id")->count();

        // 商品参数
        $warning_num = M("config")->where("conf_key = 'stock_warning' AND conf_type = 'shop'")
            ->getField("conf_value");
        $wNum = $warning_num ? to_int($warning_num) : 0;
        $goods_info["stock_warning"] = M("good")->where("stock <= $wNum AND shelves = 1 AND shop_id = $shop_id")->count();
        $goods_info["goods_num"] = M("good")->where("shelves = 1 AND shop_id = $shop_id")->count();
        $goods_info["hot_goods"] = M("good")->where("shelves = 1 AND hot = 1 AND shop_id = $shop_id")->count();
        $goods_info["recommend_goods"] = M("good")->where("shelves = 1 AND recommend = 1 AND shop_id = $shop_id")->count();

        // 代理参数
        $agent_info["agent_num"] = M("agent")->where("is_agent = 1 AND shop_id = $shop_id")->count();
        $agent_info["agent_for_audit"] = M("agent")->where("u_status = 2 AND shop_id = $shop_id")->count();
        $agent_info["agent_up_for_audit"] = M("agent_upgrade")->where("is_adopt = 0 AND shop_id = $shop_id")->count();
        $agent_info["order_nopay"] = M("agent")->where("is_pay = -1 AND shop_id = $shop_id")->count();
        $agent_info["order_calcel"] = M("agent")->where("status = -1 AND shop_id = $shop_id")->count();
        $agent_info["order_for_confirm"] = M("agent")->where("status = 1 AND shop_id = $shop_id")->count();

        // 系统参数
        $sys_info['os']            = PHP_OS;
        $sys_info['ip']            = $_SERVER['SERVER_ADDR'];
        $sys_info['web_server']    = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['php_ver']       = PHP_VERSION;
        $sys_info['mysql_ver']     = mysql_get_server_info();
        $sys_info['zlib']          = function_exists('gzclose') ? "是" : "否";
        $sys_info['timezone']      = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "未设置";
        $sys_info['socket']        = function_exists('fsockopen') ? "是" : "否";
        $sys_info['max_filesize']  = ini_get('upload_max_filesize');

        $this->assign("sys_info" ,$sys_info);
        $this->assign("goods_info" ,$goods_info);
        $this->assign("order_info" ,$order_info);
        $this->assign("agent_info" ,$agent_info);
        $this->display();
    }
    // 个人信息修改
    public function self()
    {
        if(IS_POST)
        {
            $mobile = trim(I('post.mobile'));
            $email = trim(I('post.email'));
            $pwd = trim(I('post.pwd'));
            $npwd = trim(I('post.npwd'));

            // 改密码先验证原密码是否正确
            if($pwd != '' && $npwd != ''){
                $manager = M("manager")->where("id = ".session("manager_id"))->find();
                $oldPwd = md5(md5($pwd) .$manager['salt']);
                if($oldPwd != $manager['password']){
                    $this->error("原密码错误" ,U("Index/self") ,1);
                }
                $saveData['password'] = md5(md5($npwd) .$manager['salt']);
            }
            $saveData['mobile'] = $mobile;
            $saveData['email'] = $email;
            M("manager")->where("id = ".session("manager_id"))->save($saveData);
            $this->success("修改完成" ,U("Index/self") ,1);
        }else{
            $this->self = M("manager")->where("id = ".session("manager_id"))->find();
            $this->display();
        }
    }

    public function changeTableVal()
    {
        $table = trim(I("table"));
        $id_name = trim(I("id_name"));
        $id_value = trim(I("id_value"));
        $field = trim(I("field"));
        $value = trim(I("value"));
        $where[$id_name] = $id_value;
        M($table)->where($where)->setField($field ,$value);
        $this->ajaxReturn(true);
    }

    // 首页广告
    public function index_show()
    {
        //商品列表
        $total = M('index_ad')->where(array("shop_id"=>session('shop_id')))->count();
        $page = set_page($total, C('PAGE_NUM'));
        $show = $page->show();
        $ads = M('index_ad')->where(array("shop_id"=>session('shop_id')))->limit($page->firstRow.','.$page->listRows)->order('ad_code')->select();

        $this->assign('page',$show);
        $this->assign('total',$total);
        $this->assign("ads" ,$ads);
        $this->display();
    }

    // 广告位删除
    public function ad_del()
    {
        $ad_id = to_int(I('get.ad_id'));
        $pic_path = M("index_ad")->where("ad_id = %d",array($ad_id))->getField("ad_pic");
        M("index_ad")->where("ad_id = %d",array($ad_id))->delete();
        @unlink($pic_path);
        $this->ajaxReturn(array("status" => "success"));
    }

    // 添加广告位
    public function ad_set()
    {
        if(IS_POST)
        {
            $ad_code = trim(I("post.ad_code"));
            $ad_pic = trim(I("post.ad_pic"));
            $ad_name = trim(I("post.ad_name"));
            $ad_link = trim(I("post.ad_link"));
            $ad_weight = to_int(I("post.ad_weight"));

            $flag = preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\"])*$/", $ad_link);
            if(!$flag) {
                $this->error("网址有误!");
            }

            if(!$ad_code || !$ad_pic || !$ad_name || !$ad_link || !$ad_weight){
                $this->success("信息填写有误" ,U("Index/ad_set") ,2);
            }
            switch($ad_code){
                case "banner": $ad_position = "滚动图";break;
                case "brand": $ad_position = "品牌栏";break;
                case "lattice": $ad_position = "九宫格";break;
                case "list": $ad_position = "大图列表";break;
                default : $ad_code = "list";$ad_position = "大图列表";break;
            }
            M("index_ad")->add(array(
                "ad_code" => $ad_code,
                "ad_position" => $ad_position,
                "ad_pic" => $ad_pic,
                "ad_name" => $ad_name,
                "ad_link" => $ad_link,
                "ad_weight" => $ad_weight,
                "ad_status" => 1,
                "shop_id" =>session("shop_id")
            ));
            $this->success("操作成功" ,U("Index/index_show") ,1);
        }else{
            $this->display();
        }
    }

    // 广告位编辑
    public function ad_edit()
    {
        if(IS_POST)
        {
            $ad_code = trim(I("post.ad_code"));
            $ad_pic = trim(I("post.ad_pic"));
            $ad_name = trim(I("post.ad_name"));
            $ad_link = trim(I("post.ad_link"));
            $ad_weight = to_int(I("post.ad_weight"));
            $ad_id = to_int(I("post.ad_id"));
            if(!$ad_code || !$ad_pic || !$ad_name || !$ad_link || !$ad_weight){
                $this->success("信息填写有误" ,$_SERVER['HTTP_REFERER'] ,2);
            }
            switch($ad_code){
                case "banner": $ad_position = "滚动图";break;
                case "brand": $ad_position = "品牌栏";break;
                case "lattice": $ad_position = "九宫格";break;
                case "list": $ad_position = "大图列表";break;
                default : $ad_code = "list";$ad_position = "大图列表";break;
            }
            M("index_ad")->where("ad_id = %d" ,array($ad_id))->save(array(
                "ad_code" => $ad_code,
                "ad_position" => $ad_position,
                "ad_pic" => $ad_pic,
                "ad_name" => $ad_name,
                "ad_link" => $ad_link,
                "ad_weight" => $ad_weight,
            ));
            $this->success("操作成功" ,U("Index/index_show") ,1);
        }else{
            $ad_id = to_int(I("get.id"));
            $ad = M("index_ad")->where("ad_id = %d" ,array($ad_id))->find();
            $this->assign("ad" ,$ad);
            $this->display();
        }
    }

    // 关于我们
    public function about()
    {
        $this->display();
    }

    public function ajax_msg(){
        if(IS_AJAX){
            $res['ready_send'] = M('order')->where(array('is_platform'=>1,'is_deliver'=>-1,'is_pay'=>1))->count();
            $res['ready_back'] = M('back_order')->where(array('order_status' => 1))->count();
            if(!empty($res)){
                $this->ajaxReturn($res);
            }else{
                $this->ajaxReturn(null);
            }
        }
    }

    public function no_auth(){
        $this->display();
    }

    public function developinga(){
        $this->display("developing");
    }
    public function developingb(){
        $this->display("developing");
    }
    public function developingc(){
        $this->display("developing");
    }
    public function developingd(){
        $this->display("developing");
    }
    public function developinge(){
        $this->display("developing");
    }
    public function developingf(){
        $this->display("developing");
    }
    public function developingg(){
        $this->display("developing");
    }
    public function developingh(){
        $this->display("developing");
    }
    public function developingi(){
        $this->display("developing");
    }

}